<?php
/**
 * Created by PhpStorm.
 * User: 顾永海
 * Date: 2016/10/22
 * Time: 12:35
 */

namespace Admin\Controller;


class KanJiaController extends CommonController
{

    /**
     * 直营产品列表
     * */
    public function index()
    {
        $map = array();
        if (I('get.keyword') != '') {
            $map['ktitle'] = array('like', '%' . I('get.keyword') . '%');
        }

        $list = D('KanJia')->getList($map);
        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page',$list['show']);
        $this->display();
    }
    /* *
         * 添加或者修改直营产品
         * gyh
         *
         */
    public function edit()
    {
        if (IS_POST) {
            $msg = array('errno' => 1, 'error' => '编辑失败，请重新编辑', 'url' => '');
            $data = I('post.');
            $action = I('action', '');

            if ($action == 'add') {
                unset($data['action']);
                $nameInfo = D('KanJia')->getOne(array('ktitle'=>$data['ktitle']));//查看砍价产品是否存在
                if ($nameInfo) {
                    $msg ['errno'] = 1;
                    $msg ['error'] = '该砍价产品已存在';
                    $this->ajaxReturn($msg);
                }
                $data['create_time'] = time();//添加时间
                $id = D('KanJia')->insert($data);//添加数据
                if ($id) {
                    $msg ['errno'] = 0;
                    $msg ['error'] = '添加成功！';
                    $msg ['url'] = U('index');
                }
            } else if ($action == 'edit') {
                $data['id'] = I('id');
                $nameInfo = D('KanJia')->getOne(array('ktitle'=>$data['ktitle'],'id'=>array('neq',$data['id'])));//查看直营产品是否存在
                if ($nameInfo) {
                    $msg ['errno'] = 1;
                    $msg ['error'] = '该砍价产品已存在';
                    $this->ajaxReturn($msg);
                }
                unset($data['action']);
                D('KanJia')->edit($data);//更新数据
                $msg ['errno'] = 0;
                $msg ['error'] = '编辑成功！';
                $msg ['url'] = U('index');
            } else {
                $msg ['errno'] = 1;
                $msg ['error'] = '数据异常';
                $this->ajaxReturn($msg);
            }
            $this->ajaxReturn($msg);
        } else {
            $action = I('action', '');
            if ($action == 'edit') {
                $data['id'] = I('id');
                $info = D('KanJia')->getOne($data);//获取直营产品
                $this->assign('info', $info);
            }
            $this->display();
        }
    }

    /**
     * 删除直营店铺
     *
     */
    public function del()
    {
        $ids = I('ids');
        $msg ['errno'] = 0;
        $msg ['error'] = "删除成功";
        if (is_array($ids)) {
            foreach ($ids as $id) {
                $data['id'] = $id;
                $flag = D('KanJia')->del($data);
            }
        }
        if (!$flag) {
            $msg ['errno'] = 1;
            $msg ['error'] = "删除失败！";
        }

        $this->ajaxReturn($msg);
    }


    /**
     * 店铺订单列表
     * */
    public function orderList()
    {
        $map = array();
        $map['p.status'] =2;
        if (I('get.keyword') != '') {
            $map['p.product_name'] = array('like', '%' . I('get.keyword') . '%');
        }
//        $map['s.status'] = 2;
        $list = D('KanJia')->getList($map);
       
        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page',$list['show']);
        $this->display();
    }
}